<?php

namespace Admin\Controller;

use Think\Exception;

/*
create table bbs_content_comment
(
    id             int auto_increment primary key,
    bbs_content_id int           not null default 0 comment '所属帖子id',
    title          varchar(255)  not null default '0' comment '评论标题',
    content        varchar(1999) not null default '0' comment '帖子内容',
    img        varchar(999) not null default '0' comment '图片',
    video        varchar(255) not null default '0' comment '视频',
    user_id        int           not null default 0 comment '作者ID',
    pid            int           not null default 0 comment '上级评论id',
    status         tinyint       not null default 0 comment '状态,0:审核中,1:正常,9:其他状态',
    create_time    datetime comment '创建时间',
    update_time    timestamp comment '修改时间'
) comment '论坛评论表';
 * */


class BbsContentCommentController extends BaseController
{
    public function _initialize()
    {
        if (method_exists(get_parent_class(), '_initialize')) {
            parent::_initialize(); // TODO: Change the autogenerated stub
        }
        $this->model = 'bbs_comment';
    }

    /*列表
     *
     * */
    public function index()
    {
        try {
            /*----------------------------------------凑查询条件-------------------------------------------------------*/
            $where = array();


            $where = array_filter($where);
            /*---------------------------------------开始查询----------------------------------------------------------*/
            $model = D($this->model);
            $data = $model
                ->where($where)
                ->select();
            if (!is_array($data)) {
                throw new Exception('查询失败!' . $model->getError(), 1);
            }
            $this->arr['msg'] = I('msg') ? I('msg') : '查询成功';
            $this->arr['code'] = 1;
            $this->arr['data'] = $data;
        } catch (Exception $e) {
            $this->arr['msg'] = $e->getMessage();
        }
        $this->ajaxReturn($this->arr);
    }

    /*更新
     *
     * */
    public function update()
    {
        try {
            $id = I('id');
            if (!$id) {
                throw new Exception('缺少id!', 1);
            }
            $model = D($this->model);
            $data = $model->find($id);
            if (!is_array($data) || empty($data)) {
                throw new Exception('id有误,或数据为空', 2);
            }
            if (IS_GET) {
                $this->arr['data'] = $data;
            } elseif (IS_POST) {
                if (!$model->create() || !$model->where(array('id' => $id))->save()) {
                    throw new Exception('保存出错!', 3);
                }
                $this->arr['code'] = 1;
                $this->arr['msg'] = I('msg') ? I('msg') : '保存成功!' . $model->getLastInsID();
            } else {
                $this->arr['msg'] = '仅接受get和post传输, get查询,post提交! 其他方式一律不接受!';
            }
        } catch (Exception $e) {
            $this->arr['msg'] = $e->getMessage();
        }
        $this->ajaxReturn($this->arr);
    }


    /*新增
     *
     * */
    public function add()
    {
        try {
            $model = D($this->model);
            if (!$model->create() || !$model->add()) {
                throw new Exception('新增失败,' . $model->getError());
            }
            $this->arr['msg'] = I('msg') ? I('msg') : '新增成功,' . $model->getLastInsID();
            $this->arr['code'] = 1;
        } catch (Exception $e) {
            $this->arr['msg'] = $e->getMessage();
        }
        $this->ajaxReturn($this->arr);
    }

    /*列表
     *times
     * */
    public function del()
    {
        $a = new $this;
        try {
            $id = I('id');
            if (!$id) {
                throw new Exception('缺少id', 1);
            }
            $model = D($this->model);
            if ($nums = $model->delete($id) === false) {
                throw new Exception('操作失败,' . $model->getError(), 1);
            }
            $this->arr['msg'] = I('msg') ? I('msg') : '删除' . $nums . '条数据';
            $this->arr['code'] = 1;

        } catch (Exception $e) {
            $this->arr['msg'] = $e->getMessage();
        }
        $this->ajaxReturn($this->arr);
    }
}